package com.edais.service.imp.ldap;

import com.edais.domain.ldap.SystemSetting;
import com.edais.domain.ldap.SystemSettingAttributesMapper;
import com.edais.service.ldap.LdapService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.filter.AndFilter;
import org.springframework.ldap.filter.EqualsFilter;
import org.springframework.stereotype.Service;

import javax.naming.NamingException;
import java.util.List;

/**
 * Created by chenshufeng on 2017/2/14.
 */
@Service
public class LdapServiceImpl implements LdapService{

    private static final Log logger = LogFactory.getLog(LdapServiceImpl.class);

    /**
     * ldap操作模板
     */
    @Autowired
    protected LdapTemplate ldapTemplate;

    @Override
    public SystemSetting getSystemSetting(String name) {
        SystemSetting systemSetting = null;
        try {
            //查询条件
            AndFilter andFilter = new AndFilter();
            andFilter.and(new EqualsFilter("objectclass", "systemSetting"));//对应ldap里面的objectclass属性
            andFilter.and(new EqualsFilter("systemSettingName", name));//对应ldap里面的systemSettingName属性
            //查询，可以参考spring ldap 开发手册
            List<SystemSetting> search = ldapTemplate.search("", andFilter.encode(), new SystemSettingAttributesMapper());
            if ((search != null) && !search.isEmpty()) {
                systemSetting = search.get(0);
            }
        } catch (Exception e) {
            logger.error("ldap search error ", e);
        }
        return systemSetting;
    }

}
